package org.example.flinksql.test.custom;

import org.apache.flink.table.functions.ScalarFunction;
import org.example.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.LocalDateTime;

public class TransDatetimeFunc extends ScalarFunction {
    private final Logger logger = LoggerFactory.getLogger(TransDatetimeFunc.class);

    public LocalDateTime eval(String str) {
        try {
            return Utils.str2Datetime(str);
        } catch (Exception e) {
            logger.warn(String.format("%s :: %s :: [%s]", e.getMessage(), e.getCause(), str));
            return null;
        }
    }

    public LocalDateTime eval(Long timestamp) {
        try {
            return Utils.timestamp2Datetime(timestamp);
        } catch (Exception e) {
            logger.warn(String.format("%s :: %s :: [%d]", e.getMessage(), e.getCause(), timestamp));
            return null;
        }
    }

    public String eval(LocalDateTime dateTime) {
        try {
            return Utils.datetime2Str(dateTime);
        } catch (Exception e) {
            logger.warn(String.format("%s :: %s :: [%s]", e.getMessage(), e.getCause(), dateTime));
            return null;
        }
    }

}
